package week5;

import java.util.Arrays;

public class Ex49 {
    public static void main(String[] args) {
        int n = 10000;
        int[] prime = new int[n];
        int c = 0;
        boolean[] isVisit = new boolean[n+1];
        for (int i = 2; i <= n; ++i){
            if (!isVisit[i])
                prime[++c] = i;
            for (int j = 1; j <= c && i * prime[j] <= n; ++j){
                isVisit[i * prime[j]] = true;
                if (i % prime[j] == 0)
                    break;
            }
        }
        int p = 1;
        int a, b;
        char[] arr1, arr2, arr3;
        while (prime[p] < 1000)
            p++;
        for (int i = p; prime[i] < 10000; ++i){
            a = prime[i] + 3330;
            b = a + 3330;
            if (!isVisit[a] && !isVisit[b] && prime[i] != 1487){
                arr1 = String.valueOf(prime[i]).toCharArray();
                arr2 = String.valueOf(a).toCharArray();
                arr3 = String.valueOf(b).toCharArray();
                Arrays.sort(arr1);
                Arrays.sort(arr2);
                Arrays.sort(arr3);
                if (String.valueOf(arr1).equals(String.valueOf(arr2)) && String.valueOf(arr1).equals(String.valueOf(arr3))){
                    System.out.println("" + prime[i] + a + b);
                    break;
                }
            }
        }
    }
}
